package server.main;

import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InputStream;

import android.net.LocalServerSocket;
import android.net.LocalSocket;
import android.net.LocalSocketAddress;

public class LocalStreamPipe {

	private LocalSocket receiver ,sender;
	private LocalServerSocket lss;


	public LocalStreamPipe(int bufSize){

		receiver = new LocalSocket();

		try {
			lss = new LocalServerSocket("Local");
			receiver.connect(new LocalSocketAddress("Local"));
			receiver.setReceiveBufferSize(bufSize);
			receiver.setSendBufferSize(bufSize);
			sender = lss.accept();
			sender.setReceiveBufferSize(bufSize);
			sender.setSendBufferSize(bufSize);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
    
	public InputStream getInputStream(){
		try {
		      return receiver.getInputStream();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
    
	public FileDescriptor getOutputFileDescriptor(){
		  return sender.getFileDescriptor();
	}
	
	public void close(){		
		try {
			receiver.close();
			lss.close();
			sender.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}